package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjBasicInfo;
import com.moji.entity.MjHomeProduct;
import com.moji.entity.MjProduct;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjHomeProductMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjHomeProductService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 首页产品详情(MjHomeProduct)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:08
 */
@Service("mjHomeProductService")
public class MjHomeProductServiceImpl extends ServiceImpl<MjHomeProductMapper, MjHomeProduct> implements MjHomeProductService {
    @Resource
    private MjHomeProductMapper mjHomeProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjHomeProduct queryById(Long id) {
        return this.mjHomeProductDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param mjHomeProduct 筛选条件
     * @return 查询结果
     */
    @Override
    public List<MjHomeProduct> queryByPage(MjHomeProduct mjHomeProduct) {
        LambdaQueryWrapper<MjHomeProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (mjHomeProduct.getProductId() != null) {
            queryWrapper.eq(MjHomeProduct::getProductId, mjHomeProduct.getProductId());
        }
        if (mjHomeProduct.getType() != null) {
            queryWrapper.eq(MjHomeProduct::getType, mjHomeProduct.getType());
        }
        queryWrapper.orderByAsc(MjHomeProduct::getSort);
        queryWrapper.orderByDesc(MjHomeProduct::getUpdateTime);
        List<MjHomeProduct> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjHomeProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeProduct insert(MjHomeProduct mjHomeProduct) {
        mjHomeProduct.setCreateTime(new Date());
        mjHomeProduct.setUpdateTime(new Date());
        this.mjHomeProductDao.insert(mjHomeProduct);
        return mjHomeProduct;
    }

    /**
     * 修改数据
     *
     * @param mjHomeProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeProduct update(MjHomeProduct mjHomeProduct) {
        mjHomeProduct.setUpdateTime(new Date());
        LambdaQueryWrapper<MjHomeProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjHomeProduct::getId,mjHomeProduct.getId());
        this.mjHomeProductDao.update(mjHomeProduct,queryWrapper);
//        this.mjHomeProductDao.update(mjHomeProduct);
        return this.queryById(mjHomeProduct.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjHomeProductDao.deleteById(id) > 0;
    }
}
